package com.example.androidhw04;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

	Paint mPaint;
	Bitmap mBitmap;
	Bitmap oriBitmap;

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public MyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private void init(Context context) {
		// TODO Auto-generated method stub
		mPaint = new Paint();
//		mPaint.setColor(Color.RED);

		InputStream is = context.getResources().openRawResource(R.raw.penguins);
		mBitmap = BitmapFactory.decodeStream(is);
		Bitmap scaleBitmap = Bitmap.createScaledBitmap(mBitmap, 300, 300, false);
		mBitmap.recycle();
		mBitmap = scaleBitmap;
		oriBitmap = mBitmap;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawBitmap(mBitmap, 10, 10, mPaint);
	}

	public void setBlackWhite() {
		ColorMatrix cm = new ColorMatrix();
		cm.setSaturation(0);
		ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);

		mPaint.setColorFilter(filter);
		invalidate();
	}

	public void setReverse() {
		float[] m = { -1, 0, 0, 0, 255,
				0, -1, 0, 0, 255,
				0, 0, -1, 0, 255,
				0, 0, 0, 1, 0 };

		ColorMatrixColorFilter filter = new ColorMatrixColorFilter(m);
		mPaint.setColorFilter(filter);
		invalidate();
	}
	
	public void setSepia(){
		ColorMatrix matrixA = new ColorMatrix();
		ColorMatrix matrixB = new ColorMatrix();
		matrixA.setSaturation(0);
		matrixB.setScale(1.0f, 0.95f, 0.82f, 1.0f);
		matrixA.setConcat(matrixB, matrixA);
		ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA);
		
		mPaint.setColorFilter(filter);
		invalidate();
	}
	
	public void setOriginal(){
		mPaint.setColorFilter(null);
		invalidate();
	}
}
